package com.qianwei.footpath.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.GradientDrawable;
import com.common.baselib.utils.CommonUtils;
import com.qianwei.footpath.R;

import java.lang.ref.WeakReference;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class Util {

    public static String getBMICategory(double bmi) {
        if (bmi <= 0) return "";
        if (bmi < 18.5) return "偏瘦";
        else if (bmi < 24) return "正常";
        else if (bmi < 28) return "超重";
        else return "肥胖";
    }

    /**
     * 获取体脂率标准
     *
     * @param value
     * @return
     */
    public static String getBodyFatRateCategory(float value) {
        if (value <= 0) return "";
        if (value < 10) return "偏低";
        else if (value < 20) return "正常";
        else return "偏高";
    }

    /**
     * 获取体水分率标准
     *
     * @param value
     * @return
     */
    public static String getBodyWaterRateCategory(float value) {
        if (value <= 0) return "";
        if (value < 56.2) return "偏低";
        else if (value < 68.7) return "正常";
        else return "偏高";
    }


    /**
     * 获取蛋白质标准
     *
     * @param value
     * @return
     */
    public static String getProteinCategory(float value) {
        if (value <= 0) return "";
        if (value < 10.5) return "偏低";
        else if (value < 12.8) return "正常";
        else return "偏高";
    }

    public static String getIntracellularFluidCategory(float value) {
        if (value <= 0) return "";
        if (value < 24.2) return "偏低";
        else if (value < 29.6) return "正常";
        else return "偏高";
    }

    public static String getExtracellularFluidCategory(float value) {
        if (value <= 0) return "";
        if (value < 14.8) return "偏低";
        else if (value < 18.2) return "正常";
        else return "偏高";
    }

    public static String getMuscleMassCategory(float value) {
        if (value <= 0) return "";
        if (value < 50.3) return "偏低";
        else if (value < 61.5) return "正常";
        else return "偏高";
    }

    public static String getBasalMetabolismCategory(float value) {
        if (value <= 0) return "";
        if (value < 1484) return "偏低";
        else if (value < 1814) return "正常";
        else return "偏高";
    }

    public static String getFatFreeWeightCategory(float value) {
        if (value <= 0) return "";
        if (value < 53.3) return "偏低";
        else if (value < 65.1) return "正常";
        else return "偏高";
    }

    public static String getInorganicSaltCategory(float value) {
        if (value <= 0) return "";
        if (value < 3.6) return "偏低";
        else if (value < 4.4) return "正常";
        else return "偏高";
    }

    /**
     * 计算BMI值
     *
     * @param heightCm 身高（厘米）
     * @param weightKg 体重（千克）
     * @return BMI值（保留1位小数）
     */
    public static double calculateBMIExact(double heightCm, double weightKg) {
        BigDecimal h = BigDecimal.valueOf(heightCm).divide(BigDecimal.valueOf(100));
        BigDecimal bmi = BigDecimal.valueOf(weightKg).divide(h.multiply(h), 2, RoundingMode.HALF_UP);
        return bmi.doubleValue();
    }
    public static int calculateProgress(double value, int min, int max) {
        value = Math.max(min, Math.min(value, max));
        double progress = (value - min) / (max - min) * 100;
        return (int) Math.round(progress);
    }

    public static double roundToOneDecimal(double value) {
        return Math.round(value * 10) / 10.0;
    }

    public static String[] getAppVersionInfo(Context context) {
        // 使用 WeakReference 避免内存泄漏
        WeakReference<Context> contextWeakReference = new WeakReference<>(context);
        if (contextWeakReference.get() == null) {
            return null; // 如果 Context 已被回收，返回 null
        }

        // 获取包名
        String pkName = contextWeakReference.get().getPackageName();
        String[] versionInfo = new String[2]; // 用于存储版本名称和版本代码

        try {
            // 获取 PackageInfo 对象
            PackageInfo packageInfo = contextWeakReference.get().getPackageManager().getPackageInfo(pkName, 0);
            // 获取版本名称和版本代码
            versionInfo[0] = packageInfo.versionName; // 版本名称
            versionInfo[1] = String.valueOf(packageInfo.versionCode); // 版本代码（转换为字符串）
        } catch (PackageManager.NameNotFoundException ignored) {
            // 包名未找到，返回 null
            return null;
        }

        return versionInfo;
    }


    /**
     * GradientDrawable.RECTANGLE
     * 矩形（rectangle）、椭圆形(oval)、线(line)、环形(ring)
     *
     * @param radius
     * @return
     */
    @SuppressLint("WrongConstant")
    public static GradientDrawable getImageRadiusDefault(Context context, int radius) {
        GradientDrawable gd = new GradientDrawable();
        gd.setColor(context.getResources().getColor(R.color.color_f4f4f4));
        gd.setCornerRadius(radius);
        gd.setGradientType(GradientDrawable.RECTANGLE);
        return gd;
    }

    @SuppressLint("WrongConstant")
    public static GradientDrawable getImageRadiusDefault(int radius, int color) {
        GradientDrawable gd = new GradientDrawable();
        gd.setColor(color);
        gd.setCornerRadius(radius);
        gd.setGradientType(GradientDrawable.RECTANGLE);
        return gd;
    }

    /**
     * 圆形
     *
     * @param context
     * @return
     */
    public static GradientDrawable getImageRadiusDefault(Context context) {
        GradientDrawable gd = new GradientDrawable();
        gd.setColor(context.getResources().getColor(R.color.color_f4f4f4));
        gd.setStroke(0, context.getResources().getColor(R.color.color_f4f4f4));
        gd.setShape(GradientDrawable.OVAL);
        return gd;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(float dpValue) {
        final float scale = CommonUtils.getApp().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(float pxValue) {
        final float scale = CommonUtils.getApp().getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

}
